package com.cskaoyan.service.wx.lsj;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.wx.nj.Comment;
import com.cskaoyan.bean.wx.nj.CommentExample;
import com.cskaoyan.bo.vx.wangdi.GoodsCategoryBo;
import com.cskaoyan.mapper.*;
import com.cskaoyan.mapper.wx.nj.CommentMapper;
import com.cskaoyan.vo.wx.lsj.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javafx.scene.Parent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    CskaoyanmallAdMapper cskaoyanmallAdMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    TopicMapper topicMapper;

    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    IssueMapper issueMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;

    @Autowired
    FeedbackMapper feedbackMapper;

    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;

    @Autowired
    LogMapper logMapper;

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    UserMapper userMapper;
    @Override
    public HomeIndexVO index() {

//      创建用于存放最后结果的HomeIndexVo
        HomeIndexVO homeIndexVO = new HomeIndexVO();

//     newGoodsListBean 新货list
//        创建用于存放的list
        ArrayList<HomeIndexVO.NewGoodsListBean> newGoodsList = new ArrayList<>();
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andIsNewEqualTo(true);
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);

        for (Goods good : goods) {
            HomeIndexVO.NewGoodsListBean newGoodsListBean = new HomeIndexVO.NewGoodsListBean();
            newGoodsListBean.setId(good.getId());
            newGoodsListBean.setName(good.getName());
            newGoodsListBean.setBrief(good.getBrief());
            newGoodsListBean.setPicUrl(good.getPicUrl());
            newGoodsListBean.setIsNew(good.getIsNew());
            newGoodsListBean.setIsHot(good.getIsHot());
            newGoodsListBean.setCounterPrice(good.getBrandId());
            newGoodsListBean.setRetailPrice(good.getRetailPrice().intValue());
            newGoodsList.add(newGoodsListBean);
        }
        homeIndexVO.setNewGoodsList(newGoodsList);

//        存放coupon优惠券的list
        ArrayList<HomeIndexVO.CouponListBean> couponList = new ArrayList<>();
        CouponExample couponExample = new CouponExample();
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        for (Coupon coupon : coupons) {
            HomeIndexVO.CouponListBean couponListBean = new HomeIndexVO.CouponListBean();
            couponListBean.setId(coupon.getId());
            couponListBean.setName(coupon.getName());
            couponListBean.setDesc(coupon.getDesc());
            couponListBean.setTag(coupon.getTag());
            couponListBean.setDiscount(coupon.getDiscount().intValue());
            couponListBean.setMin(coupon.getMin().intValue());
            couponListBean.setDays(coupon.getDays());
            couponList.add(couponListBean);
        }
        homeIndexVO.setCouponList(couponList);

//      存放一級商品類目的channel
        ArrayList<HomeIndexVO.ChannelBean> channel = new ArrayList<>();
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria1 = categoryExample.createCriteria();
        criteria1.andLevelEqualTo("L1");
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        for (Category category : categories) {
            HomeIndexVO.ChannelBean channelBean = new HomeIndexVO.ChannelBean();
            channelBean.setId(category.getId());
            channelBean.setName(category.getName());
            channelBean.setIconUrl(category.getIconUrl());
            channel.add(channelBean);
        }
        homeIndexVO.setChannel(channel);

//        團購部分的内容數據庫裏沒有寫暫時忽略

//        banner部分的内容
        ArrayList<HomeIndexVO.BannerBean> banner = new ArrayList<>();
        CskaoyanmallAdExample cskaoyanmallAdExample = new CskaoyanmallAdExample();
        List<CskaoyanmallAd> cskaoyanmallAds = cskaoyanmallAdMapper.selectByExample(cskaoyanmallAdExample);
        for (CskaoyanmallAd cskaoyanmallAd : cskaoyanmallAds) {
            HomeIndexVO.BannerBean bannerBean = new HomeIndexVO.BannerBean();
            bannerBean.setId(cskaoyanmallAd.getId());
            bannerBean.setName(cskaoyanmallAd.getName());
            bannerBean.setLink(cskaoyanmallAd.getLink());
            bannerBean.setUrl(cskaoyanmallAd.getUrl());
            bannerBean.setPosition(cskaoyanmallAd.getPosition());
            bannerBean.setContent(cskaoyanmallAd.getContent());
            bannerBean.setEnabled(cskaoyanmallAd.getEnabled());
            bannerBean.setAddTime(cskaoyanmallAd.getContent());
            bannerBean.setUpdateTime(cskaoyanmallAd.getContent());
            bannerBean.setDeleted(cskaoyanmallAd.getDeleted());
            banner.add(bannerBean);
        }
        homeIndexVO.setBanner(banner);

//        bannerList的内容
        ArrayList<HomeIndexVO.BrandListBean> brandList = new ArrayList<>();
        BrandExample brandExample = new BrandExample();
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        for (int i = 0; i < 4; i++) {
            HomeIndexVO.BrandListBean brandListBean = new HomeIndexVO.BrandListBean();
            brandListBean.setId(brands.get(i).getId());
            brandListBean.setName(brands.get(i).getName());
            brandListBean.setDesc(brands.get(i).getDesc());
            brandListBean.setPicUrl(brands.get(i).getPicUrl());
            brandListBean.setFloorPrice(brands.get(i).getFloorPrice().intValue());
            brandList.add(brandListBean);
        }
        homeIndexVO.setBrandList(brandList);

//      hotgoodlist
        ArrayList<HomeIndexVO.HotGoodsListBean> hotGoodsList = new ArrayList<>();
        GoodsExample goodsExample1 = new GoodsExample();
        GoodsExample.Criteria criteria2 = goodsExample1.createCriteria();
        criteria2.andIsHotEqualTo(true);
        List<Goods> goods1 = goodsMapper.selectByExample(goodsExample);

        for (Goods good : goods1) {
            HomeIndexVO.HotGoodsListBean hotGoodsListBean = new HomeIndexVO.HotGoodsListBean();
            hotGoodsListBean.setId(good.getId());
            hotGoodsListBean.setName(good.getName());
            hotGoodsListBean.setBrief(good.getBrief());
            hotGoodsListBean.setPicUrl(good.getPicUrl());
            hotGoodsListBean.setIsNew(good.getIsNew());
            hotGoodsListBean.setIsHot(good.getIsHot());
            hotGoodsListBean.setCounterPrice(good.getBrandId());
            hotGoodsListBean.setRetailPrice(good.getRetailPrice().intValue());
            hotGoodsList.add(hotGoodsListBean);
        }
        homeIndexVO.setHotGoodsList(hotGoodsList);

//      topicList 传入一个参数
        ArrayList<HomeIndexVO.TopicListBean> topicList = new ArrayList<>();
        TopicExample topicExample = new TopicExample();
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        for (int i = 0; i < 4; i++) {
            HomeIndexVO.TopicListBean topicListBean = new HomeIndexVO.TopicListBean();
            topicListBean.setId(topics.get(i).getId());
            topicListBean.setTitle(topics.get(i).getTitle());
            topicListBean.setSubtitle(topics.get(i).getSubtitle());
            topicListBean.setPrice(topics.get(i).getPrice().intValue());
            topicListBean.setReadCount(topics.get(i).getReadCount());
            topicListBean.setPicUrl(topics.get(i).getPicUrl());
            topicList.add(topicListBean);
        }
        homeIndexVO.setTopicList(topicList);

//        floorGoodsList的实现
//        ArrayList<HomeIndexVO.FloorGoodsListBean> floorGoodsList = new ArrayList<>();
//        CategoryExample categoryExample1 = new CategoryExample();
//        CategoryExample.Criteria criteria3 = categoryExample1.createCriteria();
//        criteria3.andLevelEqualTo("L1");
//        List<Category> categories1 = categoryMapper.selectByExample(categoryExample1);
//        for (Category category : categories1) {
//            HomeIndexVO.FloorGoodsListBean floorGoodsListBean = new HomeIndexVO.FloorGoodsListBean();
////            往goodsList里面加入
////            List<HomeIndexVO.FloorGoodsListBean.goodsList> goodsList = floorGoodsListBean.getGoodsList();
//            ArrayList<HomeIndexVO.FloorGoodsListBean.goodsList> goodsList = new ArrayList<>();
//
////            新增部分
//            CategoryExample categoryExample2 = new CategoryExample();
//            categoryExample2.createCriteria().andPidEqualTo(category.getId());
//            List<Category> categories2 = categoryMapper.selectByExample(categoryExample2);
//            if (categories2.size()==0){
//                continue;
//            }
////          结束
//            GoodsExample goodsExample2 = new GoodsExample();
//            goodsExample2.createCriteria().andCategoryIdEqualTo(categories2.get(0).getId());
//            List<Goods> goods2 = goodsMapper.selectByExample(goodsExample2);
//            for (Goods goods3 : goods2) {
//                HomeIndexVO.FloorGoodsListBean.goodsList goodsList1 = new HomeIndexVO.FloorGoodsListBean.goodsList();
//                goodsList1.setName(goods3.getName());
//                goodsList1.setId(goods3.getId());
//                goodsList1.setBrief(goods3.getBrief());
//                goodsList1.setPicUrl(goods3.getPicUrl());
//                goodsList1.setNew(goods3.getIsNew());
//                goodsList1.setHot(goods3.getIsHot());
//                goodsList1.setCounterPrice(goods3.getBrandId());
//                goodsList1.setRetailPrice(goods3.getRetailPrice().intValue());
//                if (goodsList.size()<4){
//                    goodsList.add(goodsList1);
//                }
//            }
//
//            floorGoodsListBean.setGoodsList(goodsList);
////            查询L2成功,就放入
//            floorGoodsList.add(floorGoodsListBean);
//            floorGoodsListBean.setName(category.getName());
//            floorGoodsListBean.setId(category.getId());
//        }
//        homeIndexVO.setFloorGoodsList(floorGoodsList);

            ArrayList<HomeIndexVO.FloorGoodsListBean> floorGoodsList = new ArrayList<>();
            CategoryExample categoryExample1 = new CategoryExample();
            CategoryExample.Criteria criteria3 = categoryExample1.createCriteria();
            criteria3.andLevelEqualTo("L1");
            List<Category> categories1 = categoryMapper.selectByExample(categoryExample1);
            for (Category category : categories1) {
                HomeIndexVO.FloorGoodsListBean floorGoodsListBean = new HomeIndexVO.FloorGoodsListBean();
                floorGoodsListBean.setName(category.getName());
                floorGoodsListBean.setId(category.getId());
//            往goodsList里面加入
                List<HomeIndexVO.FloorGoodsListBean.goodsList> goodsList = floorGoodsListBean.getGoodsList();
                GoodsExample goodsExample2 = new GoodsExample();
                goodsExample2.createCriteria().andCategoryIdEqualTo(category.getId());
                List<Goods> goods2 = goodsMapper.selectByExample(goodsExample2);
                for (Goods goods3 : goods2) {
                    HomeIndexVO.FloorGoodsListBean.goodsList goodsList1 = new HomeIndexVO.FloorGoodsListBean.goodsList();
                    goodsList1.setName(goods3.getName());
                    goodsList1.setId(goods3.getId());
                    goodsList1.setBrief(goods3.getBrief());
                    goodsList1.setPicUrl(goods3.getPicUrl());
                    goodsList1.setNew(goods3.getIsNew());
                    goodsList1.setHot(goods3.getIsHot());
                    goodsList1.setCounterPrice(goods3.getBrandId());
                    goodsList1.setRetailPrice(goods3.getRetailPrice().intValue());
                    goodsList.add(goodsList1);
                }

                floorGoodsListBean.setGoodsList(goodsList);
//            查询L2成功,就放入
                floorGoodsList.add(floorGoodsListBean);
            }
            homeIndexVO.setFloorGoodsList(floorGoodsList);
        return homeIndexVO;
    }

    @Override
    public CountVO  count() {
        GoodsExample goodsExample = new GoodsExample();
        long l = goodsMapper.countByExample(goodsExample);
        CountVO countVO = new CountVO();
        countVO.setGoodsCount((int)l);
        return countVO;
    }

    @Override
    public GoodsCategoryBo category(Integer id) {


         GoodsCategoryBo.CurrentCategoryBean  currentBean = categoryMapper.select01(id);
        int pid = currentBean.getPid();
        GoodsCategoryBo.ParentCategoryBean parentBean = categoryMapper.select02(pid);
        List<GoodsCategoryBo.BrotherCategoryBean> brothers = categoryMapper.select03(pid);
        GoodsCategoryBo goodsCategoryBo = new GoodsCategoryBo();
        goodsCategoryBo.setParentCategory(parentBean);
        goodsCategoryBo.setCurrentCategory(currentBean);
        goodsCategoryBo.setBrotherCategory(brothers);
        return goodsCategoryBo;

//    //根据id查出对应的L2数据,然后根据这个对象的pid,再查出pid相同的数据,再用这个pid去查id
//        CategoryVO categoryVO = new CategoryVO();
//
//        CategoryExample categoryExample = new CategoryExample();
//        categoryExample.createCriteria().andIdEqualTo(id);
//        List<Category> parentcategories = categoryMapper.selectByExample(categoryExample);
//
////        这个是parentcateries
//
//        CategoryVO.ParentCategoryBean parentCategoryBean = new CategoryVO.ParentCategoryBean();
////        留存pid用于查询其他brother
//
//        for (Category category : parentcategories) {
//            parentCategoryBean.setId(category.getId());
//            parentCategoryBean.setName(category.getName());
//            parentCategoryBean.setKeywords(category.getKeywords());
//            parentCategoryBean.setDesc(category.getDesc());
//            parentCategoryBean.setPid(category.getPid());
//            parentCategoryBean.setIconUrl(category.getIconUrl());
//            parentCategoryBean.setPicUrl(category.getPicUrl());
//            parentCategoryBean.setLevel(category.getLevel());
//            parentCategoryBean.setSortOrder(category.getSortOrder());
//            parentCategoryBean.setAddTime(category.getAddTime().toString());
//            parentCategoryBean.setUpdateTime(category.getUpdateTime().toString());
//            parentCategoryBean.setDeleted(category.getDeleted());
//
//        }
//        categoryVO.setParentCategory(parentCategoryBean);
//        int pid = id;
////        brotherCategory
//
//        CategoryExample brothercategoryExample = new CategoryExample();
//        brothercategoryExample.createCriteria().andPidEqualTo(pid);
//        List<Category> brotherCategory = categoryMapper.selectByExample(brothercategoryExample);
//        int first = 0;
//
//        ArrayList<CategoryVO.BrotherCategoryBean> brotherCategoryBeans = new ArrayList<>();
//        for (Category category : brotherCategory) {
//
//            if (first==0){
//                CategoryVO.CurrentCategoryBean currentCategoryBean = new CategoryVO.CurrentCategoryBean();
//                currentCategoryBean.setId(category.getId());
//                currentCategoryBean.setName(category.getName());
//                currentCategoryBean.setKeywords(category.getKeywords());
//                currentCategoryBean.setDesc(category.getDesc());
//                currentCategoryBean.setPid(category.getPid());
//                currentCategoryBean.setIconUrl(category.getIconUrl());
//                currentCategoryBean.setPicUrl(category.getPicUrl());
//                currentCategoryBean.setLevel(category.getLevel());
//                currentCategoryBean.setSortOrder(category.getSortOrder());
//                currentCategoryBean.setAddTime(category.getAddTime().toString());
//                currentCategoryBean.setUpdateTime(category.getUpdateTime().toString());
//                currentCategoryBean.setDeleted(category.getDeleted());
//                categoryVO.setCurrentCategory(currentCategoryBean);
//                first=first+1;
//            }
//            CategoryVO.BrotherCategoryBean brotherCategoryBean = new CategoryVO.BrotherCategoryBean();
//            brotherCategoryBean.setId(category.getId());
//            brotherCategoryBean.setName(category.getName());
//            brotherCategoryBean.setKeywords(category.getKeywords());
//            brotherCategoryBean.setDesc(category.getDesc());
//            brotherCategoryBean.setPid(category.getPid());
//            brotherCategoryBean.setIconUrl(category.getIconUrl());
//            brotherCategoryBean.setPicUrl(category.getPicUrl());
//            brotherCategoryBean.setLevel(category.getLevel());
//            brotherCategoryBean.setSortOrder(category.getSortOrder());
//            brotherCategoryBean.setAddTime(category.getAddTime().toString());
//            brotherCategoryBean.setUpdateTime(category.getUpdateTime().toString());
//            brotherCategoryBean.setDeleted(category.getDeleted());
//            brotherCategoryBeans.add(brotherCategoryBean);
//        }
//        categoryVO.setBrotherCategory(brotherCategoryBeans);
//
//        return categoryVO;
    }

    @Override
    public ListVO list(Integer categoryId, Integer page, Integer size,String keyword,String sort,String order) {
        //用于存放最后的结果
        ListVO listVO = new ListVO();
        PageHelper.startPage(page,size);

        GoodsExample goodsExample = new GoodsExample();
        if(sort != null && order != null) {
            goodsExample.setOrderByClause(sort + " " + order);
        }
        GoodsExample.Criteria criteria =goodsExample.createCriteria();
        if (categoryId!=0){
            criteria.andCategoryIdEqualTo(categoryId);
        }
        criteria.andDeletedEqualTo(false);
        if (keyword != null && !"".equals(keyword)){
            criteria.andNameLike("%"+keyword+"%");
        }
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);


        ArrayList<ListVO.GoodsListBean> goodsListBeans = new ArrayList<>();
        for (Goods good : goods) {
            ListVO.GoodsListBean goodsListBean = new ListVO.GoodsListBean();
            goodsListBean.setId(good.getId());
            goodsListBean.setName(good.getName());
            goodsListBean.setBrief(good.getBrief());
            goodsListBean.setPicUrl(good.getPicUrl());
            goodsListBean.setIsNew(good.getIsNew());
            goodsListBean.setIsHot(good.getIsHot());
            goodsListBean.setCounterPrice(good.getBrandId());
            goodsListBean.setRetailPrice(good.getRetailPrice().intValue());
            goodsListBeans.add(goodsListBean);
        }
        listVO.setGoodsList(goodsListBeans);

        listVO.setCount(goodsListBeans.size());
        return listVO;
    }

    @Override
    public GoodsDetailVO detail(Integer id) {
//     首先获取最后的结果
        GoodsDetailVO goodsDetailVO = new GoodsDetailVO();

//      然后就是spec查询
        ArrayList<GoodsDetailVO.SpecificationListBean> specificationListBeans = new ArrayList<>();
        GoodsSpecificationExample goodsSpecificationExample = new GoodsSpecificationExample();
        goodsSpecificationExample.createCriteria().andGoodsIdEqualTo(id);
        List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.selectByExample(goodsSpecificationExample);

        ArrayList<GoodsDetailVO.SpecificationListBean.ValueListBean> valueListBeans = new ArrayList<>();
        GoodsDetailVO.SpecificationListBean specificationListBean = new GoodsDetailVO.SpecificationListBean();
        for (GoodsSpecification goodsSpecification : goodsSpecifications) {
            specificationListBean.setName(goodsSpecification.getSpecification());
            GoodsDetailVO.SpecificationListBean.ValueListBean valueListBean = new GoodsDetailVO.SpecificationListBean.ValueListBean();
            valueListBean.setId(goodsSpecification.getId());
            valueListBean.setGoodsId(goodsSpecification.getGoodsId());
            valueListBean.setSpecification(goodsSpecification.getSpecification());
            valueListBean.setValue(goodsSpecification.getValue());
            valueListBean.setPicUrl(goodsSpecification.getPicUrl());
            valueListBean.setAddTime(goodsSpecification.getAddTime().toString());
            valueListBean.setUpdateTime(goodsSpecification.getUpdateTime().toString());
            valueListBean.setDeleted(goodsSpecification.getDeleted());
            valueListBeans.add(valueListBean);
        }
        specificationListBean.setValueList(valueListBeans);
        specificationListBeans.add(specificationListBean);
        goodsDetailVO.setSpecificationList(specificationListBeans);

//      查询issue
        ArrayList<GoodsDetailVO.IssueBean> issueBeans = new ArrayList<>();
        IssueExample issueExample = new IssueExample();
        List<Issue> issues = issueMapper.selectByExample(issueExample);
        for (Issue issue : issues) {
            GoodsDetailVO.IssueBean issueBean = new GoodsDetailVO.IssueBean();
            issueBean.setId(issue.getId());
            issueBean.setQuestion(issue.getQuestion());
            issueBean.setAnswer(issue.getAnswer());
            issueBean.setAddTime(issue.getAddTime().toString());
//            issueBean.setUpdateTime(issue.getUpdateTime().toString());
            issueBean.setDeleted(issue.getDeleted());
            issueBeans.add(issueBean);
        }
        goodsDetailVO.setIssue(issueBeans);


//      productList构造,写在这个里面
        ArrayList<GoodsDetailVO.ProductListBean> productListBeans = new ArrayList<>();

//      往里面填数据
        GoodsProductExample goodsProductExample = new GoodsProductExample();
        goodsProductExample.createCriteria().andGoodsIdEqualTo(id).andDeletedEqualTo(false);
        List<GoodsProduct> goodsProducts = goodsProductMapper.selectByExample(goodsProductExample);
        for (GoodsProduct goodsProduct : goodsProducts) {
            GoodsDetailVO.ProductListBean productListBean = new GoodsDetailVO.ProductListBean();
            productListBean.setId(goodsProduct.getId());
            productListBean.setGoodsId(goodsProduct.getGoodsId());
            productListBean.setPrice(goodsProduct.getPrice().intValue());
            productListBean.setNumber(goodsProduct.getNumber());
            productListBean.setUrl(goodsProduct.getUrl());
            productListBean.setAddTime(goodsProduct.getAddTime().toString());
            productListBean.setUpdateTime(goodsProduct.getUpdateTime().toString());
            productListBean.setDeleted(goodsProduct.getDeleted());
            productListBean.setSpecifications(Arrays.asList(goodsProduct.getSpecifications()));
            productListBeans.add(productListBean);
        }

        goodsDetailVO.setProductList(productListBeans);

//        开始info相关的内容
        GoodsDetailVO.InfoBean infoBean = new GoodsDetailVO.InfoBean();
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andIdEqualTo(id).andDeletedEqualTo(false);
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        for (Goods good : goods) {
            infoBean.setId(good.getId());
            infoBean.setGoodsSn(good.getGoodsSn());
            infoBean.setName(good.getName());
            infoBean.setCategoryId(good.getCategoryId());
            infoBean.setBrandId(good.getBrandId());
            infoBean.setKeywords(good.getKeywords());
            infoBean.setBrief(good.getBrief());
            infoBean.setIsOnSale(good.getIsOnSale());
            infoBean.setSortOrder(good.getSortOrder());
            infoBean.setPicUrl(good.getPicUrl());
            infoBean.setShareUrl(good.getShareUrl());
            infoBean.setIsNew(good.getIsNew());
            infoBean.setIsHot(good.getIsHot());
            infoBean.setUnit(good.getUnit());
            infoBean.setCounterPrice(good.getCounterPrice().intValue());
            infoBean.setRetailPrice(good.getRetailPrice().intValue());
            infoBean.setAddTime(good.getAddTime().toString());
            infoBean.setUpdateTime(good.getUpdateTime().toString());
            infoBean.setDeleted(good.getDeleted());
            infoBean.setDetail(good.getDetail());
            infoBean.setGallery(Arrays.asList(good.getGallery()));
        }
       goodsDetailVO.setInfo(infoBean);

//        开始Arrtibute部分
        ArrayList<GoodsDetailVO.AttributeBean> attributeBeans = new ArrayList<>();

        GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
        goodsAttributeExample.createCriteria().andGoodsIdEqualTo(id);
        List<GoodsAttribute> goodsAttributes = goodsAttributeMapper.selectByExample(goodsAttributeExample);

        for (GoodsAttribute goodsAttribute : goodsAttributes) {
            GoodsDetailVO.AttributeBean attributeBean = new GoodsDetailVO.AttributeBean();
            attributeBean.setId(goodsAttribute.getId());
            attributeBean.setGoodsId(goodsAttribute.getGoodsId());
            attributeBean.setAttribute(goodsAttribute.getAttribute());
            attributeBean.setValue(goodsAttribute.getValue());
            attributeBean.setAddTime(goodsAttribute.getAddTime().toString());
            attributeBean.setUpdateTime(goodsAttribute.getUpdateTime().toString());
            attributeBean.setDeleted(goodsAttribute.getDeleted());
            attributeBeans.add(attributeBean);
        }

        goodsDetailVO.setAttribute(attributeBeans);

//        开始brand,首先根据goodsid查出brandid;
        Goods goods1 = goodsMapper.selectByPrimaryKey(id);
//       查询
        if (goods1.getBrandId()!=0){
            Brand brand = brandMapper.selectByPrimaryKey(goods1.getBrandId());

            GoodsDetailVO.BrandBean brandBean = new GoodsDetailVO.BrandBean();
            brandBean.setId(brand.getId());
            brandBean.setName(brand.getName());
            brandBean.setDesc(brand.getDesc());
            brandBean.setPicUrl(brand.getPicUrl());
            brandBean.setSortOrder(brand.getSortOrder());
            brandBean.setFloorPrice(brand.getFloorPrice().intValue());
            brandBean.setAddTime(brand.getAddTime().toString());
            brandBean.setUpdateTime(brand.getUpdateTime().toString());
            brandBean.setDeleted(brand.getDeleted());

            goodsDetailVO.setBrand(brandBean);

        }

//        goodsDetailVO.setComment();
        // goodsDetailVO.setGroupon(Collections.singletonList(new int[]{}));
       // goodsDetailVO.setShareImage("");
       // goodsDetailVO.setAttribute(Collections.singletonList(new int[]{}));
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andTypeEqualTo(Byte.valueOf("0")).andValueIdEqualTo(id);
        List<Comment> comments = commentMapper.selectByExample(commentExample);

        ArrayList<GoodsDetailVO.DataX> dataXES = new ArrayList<>();
        for (Comment comment : comments) {
            if (dataXES.size()>5){
                break;
            }
            GoodsDetailVO.DataX dataX = new GoodsDetailVO.DataX();
            dataX.setAddTime(comment.getAddTime().toString());
            dataX.setId(comment.getId());
            dataX.setContent(comment.getContent());
            dataX.setPicList(comment.getPicUrls());
            User user = userMapper.selectByPrimaryKey(comment.getUserId());
            dataX.setNickname(user.getNickname());
            dataX.setAvatar(user.getAvatar());
            dataXES.add(dataX);
        }
        GoodsDetailVO.CommentBean commentBean = new GoodsDetailVO.CommentBean();
        commentBean.setCount(comments.size());
        commentBean.setData(dataXES);
        goodsDetailVO.setComment(commentBean);


        return goodsDetailVO;
    }

    @Override
    public relatedList related(Integer id) {
//        首先创建一个结果的对象
        relatedList relatedList = new relatedList();
        ArrayList<relatedList.GoodsListBean> goodsListBeans = new ArrayList<>();

        Goods goods = goodsMapper.selectByPrimaryKey(id);
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andCategoryIdEqualTo(goods.getCategoryId());
        List<Goods> goods1 = goodsMapper.selectByExample(goodsExample);
        for (Goods goods2 : goods1) {
            com.cskaoyan.vo.wx.lsj.relatedList.GoodsListBean goodsListBean = new relatedList.GoodsListBean();
            goodsListBean.setId(goods2.getId());
            goodsListBean.setName(goods2.getName());
            goodsListBean.setBrief(goods2.getBrief());
            goodsListBean.setPicUrl(goods2.getPicUrl());
            goodsListBean.setIsNew(goods2.getIsNew());
            goodsListBean.setIsHot(goods2.getIsHot());
            goodsListBean.setCounterPrice(goods2.getCounterPrice().intValue());
            goodsListBean.setRetailPrice(goods2.getRetailPrice().intValue());
            goodsListBeans.add(goodsListBean);
        }
        relatedList.setGoodsList(goodsListBeans);

        return relatedList;
    }

    @Override
    public LoglistVO loglist(Integer page, Integer limit, String sort, String order,String name) {
//       用于存放最后的结果
        LoglistVO loglistVO = new LoglistVO();
        PageHelper.startPage(page,limit);
        LogExample logExample = new LogExample();
        logExample.setOrderByClause(sort+" "+order);
        LogExample.Criteria criteria = logExample.createCriteria().andDeletedEqualTo(false);
        if (name!=null&&!"".equals(name)){
            criteria.andAdminLike("%"+name+"%");
        }
        List<Log> logs = logMapper.selectByExample(logExample);
        ArrayList<LoglistVO.ItemsBean> itemsBeans = new ArrayList<>();

        for (Log log : logs) {
            LoglistVO.ItemsBean itemsBean = new LoglistVO.ItemsBean();
            itemsBean.setId(log.getId());
            itemsBean.setAdmin(log.getAdmin());
            itemsBean.setIp(log.getIp());
            itemsBean.setType(log.getType());
            itemsBean.setAction(log.getAction());
            itemsBean.setStatus(log.getStatus());
            itemsBean.setResult(log.getResult());
            itemsBean.setComment(log.getComment());
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd hh:mm:ss Z yyyy",
//                    Locale.ENGLISH);
           itemsBean.setAddTime(log.getAddTime().toString());
            itemsBean.setUpdateTime(log.getUpdateTime().toString());
            itemsBean.setDeleted(log.getDeleted());
            itemsBeans.add(itemsBean);
        }

        PageInfo<LoglistVO.ItemsBean> itemsBeanPageInfo = new PageInfo<>(itemsBeans);
        long total = itemsBeanPageInfo.getTotal();
        loglistVO.setTotal((int)total);
        loglistVO.setItems(itemsBeans);
        return loglistVO;
    }

    @Override
    public GoodsCategoryBo category01(Integer categoryId, Integer page, Integer size, String keyword, String sort, String order) {

        return null;
    }


}
